package com.zhu.aliyunService.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;

import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.google.common.collect.Iterators;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Component
public class VodUtils {
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    public DefaultAcsClient initClient() {
        VodProperties vodProperties = new VodProperties();
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    public static DeleteVideoResponse deleteVideoResponse(DefaultAcsClient client, String id) throws ClientException {
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(id);
        return client.getAcsResponse(request);
    }

    public static GetVideoPlayAuthResponse getVideoPlayAuthResponse(DefaultAcsClient client,String id) throws ClientException {
        GetVideoPlayAuthRequest request=new GetVideoPlayAuthRequest();
        request.setVideoId(id);
        request.setAuthInfoTimeout(200L);
        return  client.getAcsResponse(request);

    }

    /**
     * 批量删除视频
     * @param client
     * @param idList
     * @return
     * @throws ClientException
     */
    public static DeleteVideoResponse deleteVideoResponse(DefaultAcsClient client, List<String> idList) throws ClientException {
        DeleteVideoRequest request = new DeleteVideoRequest();
        StringBuffer sb = new StringBuffer();
        //阿里云最大支持一次性删除20个视频
        int size = idList.size();
        for (int i = 0; i < size; i++) {
            sb.append(idList.get(i));
            if (i == size - 1 || i % 20 == 0) {
                //删除且清空StringBuffer
                request.setVideoIds(sb.toString());
            } else {
                sb.append(",");
            }

        }
        return client.getAcsResponse(request);
    }
        @Test
        public void test () {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 30; i++) {
                list.add("1234565");
            }
            StringBuffer sb = new StringBuffer();
            int size = list.size();
            for (int i = 0; i < size; i++) {
                sb.append(list.get(i));
                if (i == size - 1 || i % 20 == 0) {
                    //删除

                    sb = new StringBuffer();
                } else {
                    sb.append(",");
                }
            }
            System.out.println(sb.toString());
        }
    }

